weiXinController.controller('kgyStorePageCtl',['$scope','$state','$stateParams','$timeout','$interval','$compile','$rootScope',function($scope,$state,$stateParams,$timeout,$interval,$compile,$rootScope) {
	// 分页初始化条件
	var param = {
        id: 'tableDemo',
        checkbox:false,
        column: [
            {
                type: 'rowNum',
                title: '编号',
                titleTdStyle: 'width: 100px;color: #17a2b8;'
            }, {
                type: 'key',
                mapping: 'id',
                show: false
            },{
            	type:'?',
				title:'账内/账外',
				trans:function(data){
					if(data.type_account === 'OUT'){
                		return '账外';
                	}
                	if(data.type_account === 'IN'){
                		return '账内';
                	}
				}
            },{
            	type:'?',
				title:'冲销/实际',
				trans:function(data){
					if(data.type === 'IN'){
                		return '实际';
                	}
					if(data.type === 'OUT'){
                		return '出库';
                	}
                	if(data.type === 'BALANCE'){
                		return '冲销';
                	}
				}
            },{
            	type:'?',
				title:'自建/同步',
				trans:function(data){
					if(data.type_from === 'IN'){
                		return '自建';
                	}
                	if(data.type_from === 'OUT_COMMON'){
                		return '同步申领单';
                	}
                	if(data.type_from === 'OUT_CANCEL'){
                		return '同步已取消入库单';
                	}
				}
            },{
            	type:'?',
				title:'状态',
				trans:function(data){
					if(data.status === 'INIT'){
                		return '未完成';
                	}
                	if(data.status === 'DOING'){
                		return '处理中';
                	}
                	if(data.status === 'DONE'){
                		return '已完成';
                	}
                	if(data.status === 'BINDED'){
                		return '已绑定';
                	}
                	if(data.status === 'BALANCED'){
                		return '已冲销';
                	}
                	if(data.status === 'CANCEL'){
                		return '已取消';
                	}
				}
            },{
            	mapping: '?',
            	title:'操作时间',
            	trans:function(data){
            		return data.gmt_modified;
            	}
            },{
                type: 'function',
                title: '操作',
                titleDivStyle: 'text-align: center;',
                //dataDivStyle: 'text-align: center',
                func: [{
                    title: '冲销',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-balance-scale"></i>',
                    text: '冲销',
                    show: function(data){
                    	// 只有冲销类型+状态为初始化的才能冲销
                    	return (data.type === 'BALANCE' && data.status === 'INIT');
                    },
                    func: function (data) {
                        $scope.storeBalance(data.id);
                    }
                },{
                    title: '打印',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-paint-roller"></i>',
                    text: '打印',
                    show: function(data){
                    	// 只有实际的才能打印
                    	return (data.type === 'IN');
                    },
                    func: function (data) {
                        $scope.storePrint(data.id);
                    }
                },{
                    title: '查看',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-eye"></i>',
                    text: '查看',
                    func: function (data) {
                        $scope.storeView(data.id);
                    }
                },{
                    title: '修改',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-pencil-alt"></i>',
                    text: '修改',
                    show: function(data){
                    	// 只有自建+状态为初始化的才能修改
                    	return (data.status === 'INIT' && data.type_from === 'IN');
                    },
                    func: function (data) {
                        $scope.toStoreEdit(data.id);
                    }
                },{
                    title: '取消',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-ban"></i>',
                    text: '取消',
                    show: function(data){
                    	// 只有自建+状态为初始化的才能取消
                    	return (data.status === 'INIT' && data.type_from === 'IN');
                    },
                    func: function (data) {
                        $scope.storeCancel(data.id);
                    }
                },{
                    title: '绑定',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-ban"></i>',
                    text: '绑定',
                    show: function(data){
                    	// 只有同步过来的+状态为初始化的才能绑定
                    	return (data.type_from === 'OUT' && data.status === 'INIT');
                    },
                    func: function (data) {
                        $scope.storeBind(data.id);
                    }
                }]
            }
        ],
        pageSize:[[2],10],
        pageConfig:{
        	showGoNum:false
        },
        page: function (cmd, currentPage, pageSize, totalPageNum) {
            if ('first' === cmd) {
                currentPage = 1;
            } else if ('prev' === cmd) {
                currentPage--;
            } else if ('next' === cmd) {
                currentPage++;
            } else if ('last' === cmd) {
                currentPage = totalPageNum;
            } else if ('change' === cmd) {
                currentPage = 1;
            }

            $scope.search(currentPage, pageSize);
        }
    };
    EdeyeAdmin.initDataTablePhone(param, {data: []});
    
    // 冲销入库单
    $scope.storeBalance = function(id){
        // TODO 二次确认 注释 4月8日
        // layer.confirm('是否冲销该入库单?', {
			// btn: ['确认','取消'],
			// closeBtn: false
        // }, function(){
			EdeyeAdmin.post({
	            url: '/storeInfo/storeBalance',
	            data: {id:id}
	        }, {
	            SUCCESS: function (ret) {
	            	EdeyeAdmin.closeMsg(ret.msg,true,1);
	            	$scope.search();
	            }
	        });
			layer.closeAll();
		// });
    }
    
    // 入库单二维码打印
    $scope.storePrint = function(id){
        // TODO 二次确认 注释 4月8日
        // layer.confirm('是否打印该入库单?', {
			// btn: ['确认','取消'],
			// closeBtn: false
        // }, function(){
			EdeyeAdmin.post({
	            url: '/storeInfo/storePrint',
	            data: {id:id}
	        }, {
	            SUCCESS: function (ret) {
	            	EdeyeAdmin.closeMsg(ret.msg,true,1);
	            }
	        });
			layer.closeAll();
		// });
    }
    
    $scope.storeRecordViewList = [];
    
    // 入库单查看
    $scope.storeView = function(id){
		EdeyeAdmin.post({
            url: '/storeInfo/storeView',
            data: {id:id}
        }, {
            SUCCESS: function (ret) {
            	var shelfs = ret.shelfs;
            	var baskets = ret.baskets;
            	var rfids = ret.rfids;
            	var storeInfoRecord = ret.storeInfoRecord;
        		$scope.storeRecordViewList = [];
            	for(var tmpKey in storeInfoRecord){
            		var shelf_show_info = '';
            		var weight_show_info = '';
            		var rfid_show_info = '';
            		for(var tmpInnerKey in shelfs){
            			if(shelfs[tmpInnerKey].id === storeInfoRecord[tmpKey].shelf_id){
            				shelf_show_info = ' - ' + shelfs[tmpInnerKey].name;
        					for(var tmpInInnerKey in shelfs){
        						if(shelfs[tmpInnerKey].pid === shelfs[tmpInInnerKey].id){
        							shelf_show_info = shelfs[tmpInInnerKey].name + shelf_show_info;
                    				break;
								}
        					}
            				break;
            			}
            		}
            		if(storeInfoRecord[tmpKey].type === 'WEIGHT'){
            			for(var tmpInnerKey in baskets){
                			if(baskets[tmpInnerKey].id === storeInfoRecord[tmpKey].device_basket_id){
                				weight_show_info = ' - ' + baskets[tmpInnerKey].name;
            					for(var tmpInInnerKey in baskets){
            						if(baskets[tmpInnerKey].device_info_id === baskets[tmpInInnerKey].id){
            							weight_show_info = baskets[tmpInInnerKey].name + weight_show_info;
                        				break;
    								}
            					}
                				break;
                			}
                		}
            		}else if(storeInfoRecord[tmpKey].type === 'RFID'){
            			var storeInfoRecordRfids = JSON.parse(storeInfoRecord[tmpKey].rfid);
            			for(var tmpInnerKey in storeInfoRecordRfids){
                			for(var tmpInInnerKey in rfids){
                				if(storeInfoRecordRfids[tmpInnerKey] === rfids[tmpInInnerKey].id){
                					rfid_show_info += rfids[tmpInInnerKey].name + ",";
                				}
                			}
            			}
            			rfid_show_info = rfid_show_info.slice(0,rfid_show_info.length - 1);
            		}
            		$scope.storeRecordViewList.push({
                        sp_name : storeInfoRecord[tmpKey].sp_name,
            			sp_code : storeInfoRecord[tmpKey].sp_code,
            			batch_code : storeInfoRecord[tmpKey].batch_code,
            			type : storeInfoRecord[tmpKey].type,
            			num : storeInfoRecord[tmpKey].num,
            			shelf_show_info:shelf_show_info,
            			weight_show_info:weight_show_info,
            			rfid_show_info:rfid_show_info
            		});
            	}
            	
            	$scope.$apply();
            	
            	// console.log(ret);
            	console.log($scope.storeRecordViewList);

                scrollTop = document.scrollingElement.scrollTop;
                EdeyeAdmin.stopScroll(scrollTop);
            	$('#viewStoreModal').modal('show');
            }
        });
    }

    var scrollTop;

    $scope.hide = function () {
        $('#viewStoreModal').modal('hide');
        EdeyeAdmin.openScroll(scrollTop);
    }
    
    // 入库单修改跳转
    $scope.toStoreEdit = function(id){
    	$state.go('/kgy/store/edit',{param:EdeyeAdmin.zip(id)});
    }
    
    // 入库单取消
    $scope.storeCancel = function(id){
        // TODO 二次确认 注释 4月8日
        // layer.confirm('是否取消该入库单?', {
			// btn: ['确认','取消'],
			// closeBtn: false
        // }, function(){
			EdeyeAdmin.post({
	            url: '/storeInfo/storeCancel',
	            data: {id:id}
	        }, {
	            SUCCESS: function (ret) {
	            	$scope.search();
	            	EdeyeAdmin.closeMsg(ret.msg,true,1);
	            }
	        });
			layer.closeAll();
		// });
    }
    
    // TODO 入库单绑定
    $scope.storeBind = function(id){
    	alert('TODO storeBind ' + id);
    }
	
	// 备件搜索条件
	$scope.storeSearchObj = {
		type_account:'',
		type:'',
		type_from:'',
		status:''
	}
	
	// 查询入库单信息
	$scope.search = function(currentPage, pageSize){
		$scope.storeSearchObj.currentPage = (currentPage === undefined ? 1 : currentPage);
		// $scope.storeSearchObj.pageSize = (pageSize === undefined ? 1 : pageSize);
		$scope.storeSearchObj.pageSize = EdeyeAdmin.getPageSizePhone('tableDemo');
		
		// TODO 测试用,用完了就删掉
		// $scope.storeSearchObj.status = 'DOING';
		
		EdeyeAdmin.post({
	        url: '/storeInfo/page',
	        data: $scope.storeSearchObj,
	    }, {
	        SUCCESS: function (ret) {
	            if (ret.code === '200') {
	            	EdeyeAdmin.setDataTableDataPhone(param, ret.data);
	            }
	        }
	    });
	}
	
	$scope.search();
}]);